#include "Coin.h"


Coin::Coin(float posX, float posY, float width, float height, Resources* res, int number, int depth)
:GameObject(posX, posY, width, height, res, number, depth)
{
	_sprite = new Sprite(_resources->Coin, 7, 7, 0.05f);
	_sprite->Next(rand() % 6);

	_id = ID::ID_Coin;
	_state = COIN_STATE::COIN_NORMAL;
	_vel_y = _acc_y = _old_y = 0;

	_isStatic = true;
	// Set nam trong hay ngoai Gach
	//
	//
}


Coin::~Coin()
{
}

void Coin::Update(float time, QNode* &Tree)
{
	switch (_state)
	{
	case COIN_STATE::COIN_NORMAL:
		break;
	case COIN_STATE::COIN_MOVEUP:
		_vel_y += _acc_y * time;
		_pos_y -= _vel_y * time + 0.5f * _acc_y * time * time;

		if (_pos_y < _old_y)
			_isAlive = false;
		break;
	default:
		break;
	}

	UpdateAnimation(time);
}

void Coin::Render(LPD3DXSPRITE spriteHandler, Viewport* viewport)
{
	if (_isAlive == true)
		_sprite->Render(spriteHandler, _pos_x, _pos_y, false, viewport);
}

void Coin::ChangeState(char state)
{
	_state = state;

	switch (_state)
	{
	case COIN_STATE::COIN_NORMAL:
		break;
	case COIN_STATE::COIN_MOVEUP:
		_old_y = _pos_y;
		_vel_y = -COIN_VY;
		_acc_y = GRAVITY * 1.5f;
		break;
	default:
		break;
	}
}

void Coin::UpdateAnimation(float time)
{
	_sprite->Next(time);
}